import pymysql

def OpenMysql():
    print("进入打开数据库函数")
    db = pymysql.connect(host="localhost", user="root", password="root", database="bilibilispider")

    curses = db.cursor()

    curses.execute("select version()")

    data = curses.fetchone()

    print(data)

    db.close()

def insertEmp():
    db = pymysql.connect(host="localhost", user="root", password="root", database="bilibilispider")

    curses = db.cursor()

    sql = "insert into ope_log(opedate,opetime,content,username) values( NOW(), NOW(), 'try', 'fhd')"
    try:
        curses.execute(sql)
        db.commit()
        print("OK")
    except:
        db.rollback()
        print("GG")
    db.close()

def selectEmp():
    db = pymysql.connect(host="localhost", user="root", password="root", database="bilibilispider")

    curses = db.cursor()
    sql = "select * from ope_log"
    try:
        curses.execute(sql)
        res = curses.fetchall()
        for row in res:
            id = row[0]
            opedate = row[1]
            opetime = row[2]
            context = row[3]
            username = row[4]
            print("id=%s,opedate=%s,opetime=%s,context=%s,username=%s" % (id,opedate,opetime,context,username))
    except:
        print("wrong")
    db.close()